package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamMakerQuery;
import com.jcfk.eam.domain.dto.base.EamMakerDTO;
import com.jcfk.eam.domain.dto.base.EamMakerContactDTO;
import com.jcfk.eam.service.base.EamMakerService;

import java.util.List;

/**
 * 制造商
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Api(tags = {"制造商管理"})
@RestController
@RequestMapping("eammaker")
public class EamMakerController {
    @Autowired
    private EamMakerService eamMakerService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamMakerQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询制造商")
    @PostMapping("/queryByPage")
    public RetPage<EamMakerDTO> queryByPage(@RequestBody ReqPageParam<EamMakerQuery> requestParam) {
        DataPage<EamMakerDTO> page = new DataPage<EamMakerDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamMakerDTO.class);
        page = eamMakerService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamMakerQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取制造商列表", notes = "获取制造商列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamMakerContactDTO>> getDetails(@RequestBody EamMakerQuery query) {
        if(query.getDeleted() == null)
            query.setDeleted(0);

        List<EamMakerContactDTO> list = eamMakerService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamMakerQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取制造商列表", notes = "获取制造商列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamMakerDTO>> getList(@RequestBody EamMakerQuery query) {
        if(query.getDeleted() == null)
            query.setDeleted(0);

        List<EamMakerDTO> list = eamMakerService.getList(query);

        return RetResult.ok(list);
    }

    /**
     *  制造商
     * 根据id 查询详情
     */
    @ApiOperation("查询制造商")
    @GetMapping("/get/{id}")
    public RetResult<EamMakerDTO> get(@PathVariable("id") String id) {
            EamMakerDTO item = eamMakerService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理制造商
     */
    @ApiOperation("保存制造商")
    @PostMapping("/save")
    @RequestLogHeader(modular = "制造商管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMakerDTO eamMaker) {
        // 数据校验
        RetResult ret = eamMakerService.isValid(eamMaker);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        EamMakerQuery query = new EamMakerQuery();
        query.setMakerCode(eamMaker.getMakerCode());
        List<EamMakerDTO> getList = eamMakerService.getList(query);
        if(getList != null && !getList.isEmpty()){
            for (EamMakerDTO set : getList){
                if(!set.getId().equals(eamMaker.getId()))
                    throw new CustomException("编号不能重复，保存失败");
            }
        }
        if (StrUtil.isEmpty(eamMaker.getId())){
            // 新增
                eamMakerService.insert(eamMaker);
        } else{
            // 更新
                eamMakerService.update(eamMaker);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除制造商
     */
    @ApiOperation("批量删除制造商")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "制造商管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMakerDTO eamMaker) {
        return RetResult.ok(eamMakerService.logicDeleteList(eamMaker));
    }

    /**
     *  删除制造商
     */
    @ApiOperation("删除制造商")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "制造商管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMakerService.deleteitem(id);
        return RetResult.ok();
    }
}
